import {WORKSTATE, workSwitch} from "./public/CreepPublicAction.js";

export const balancerRun = (creep, sourcesIndex) => {
    workSwitch(creep, RESOURCE_ENERGY)
    if (creep.memory[WORKSTATE]) {
        workCore(creep, sourcesIndex)
    } else {
        getSources(creep, sourcesIndex)
    }
}

const workCore = (creep) => {
    const terminal=creep.room.terminal
    if (creep.transfer(terminal,RESOURCE_ZYNTHIUM_BAR)===ERR_NOT_IN_RANGE){
        creep.moveTo(terminal)
    }

}

const getSources = (creep) => {
    /**
     *
     * @type {StructureFactory[]}
     */
    const factory = Game.rooms[creep.room.name].find(FIND_STRUCTURES, {filter: {structureType: STRUCTURE_FACTORY}})[0]
    if (creep.withdraw(factory, RESOURCE_ZYNTHIUM_BAR)===ERR_NOT_IN_RANGE) {
        creep.moveTo(factory)
    }
}